home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 13401 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.4 KB

  1. Path: solon.com!not-for-mail
  2. From: seebs@solutions.solon.com (Peter Seebach)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: reversing a string
  5. Date: 6 Apr 1996 14:19:40 -0600
  6. Organization: Usenet Fact Police (Undercover)
  7. Message-ID: <4k6jks$fh9@solutions.solon.com>
  8. References: <4k6cjl$j8f@central.server.swt.edu>
  9. Reply-To: seebs@solon.com
  10. NNTP-Posting-Host: solutions.solon.com
  11.  
  12. In article <4k6cjl$j8f@central.server.swt.edu>,
  13. Leland Newsom <ln16674@nyssa.swt.edu> wrote:
  14. >I have a challenge from a friend of mine.  He wanted me to reverse a string 
  15. >with recursion without using any additional variables or loops.  I got mine
  16. >to work by using exclusive or, but I needed an additional variable.  Can 
  17. >someone help with this problem without using the additional variable?
  18.  
  19. It's trivial with something like
  20.     void rev (char *s) {
  21.         if (*s) {
  22.             rev(s + 1);
  23.             putchar(*s);
  24.         }
  25.     }
  26. but this is not, strictly speaking, fair, as you're using external state
  27. anyway.
  28.  
  29. I can't see a way to reverse in place without a temporary of some sort,
  30. or a loop of some sort, or something which is fundementally equivalent
  31. to one of those.  There may be one, but I don't know it.
  32.  
  33. -s
  34. -- 
  35. Peter Seebach - seebs@solon.com - Copyright 1996 Peter Seebach.
  36. C/Unix wizard -- C/Unix questions? Send mail for help.  No, really!
  37. FUCK the communications decency act.  Goddamned government.  [literally.]
  38. The *other* C FAQ - http://www.solon.com/~seebs/c/c-iaq.html
  39.